  function priorpdf=priordens(param,prior,alphap,betap,lowerb,upperb)
% function priorpdf=priordens(param,prior,lcone,lctwo) 
%
% Computes the  pdf of the prior distributions for each
%   entry of the parameter vector PARAM which is not 
%   calibrated, as indicated by the control matrix 
%   PRIOR    
% ALPHAP & BETAP have the location parameters 
%          of each density (alpha, beta) 
%          not the mean and standard deviation 
% 
% Output 
% ------
% priorpdf  vector with columns containing logpriors for 
%           N | W | G | B | I  
%
% Note: assumes thet the input for the IW or the IG1 is the 
%       standard deviation not the variance 
%       Not defined for a uniform density 

% Modified 5/4/04   
% Explicitly added the pdf for the uniform 
% 3/22/2006
% 1/13/09   Need pass LOWERB and UPPERB to estimate density of the
% Truncated Beta 
% 
% Output is SUM of LOGS of the individual densities
% Corrected July 2010 
% --------------------------------------------------------
priorpdf=zeros(1,8);

% ===============================================
% Pdf of a Multivariate normal under independence
% ===============================================
numn=prior(1,1); 
if numn > 0; 
    mu=alphap(prior(2:numn+1,1));     %Mean 
    sigma=betap( prior(2:numn+1,1) ); %Need the variance
    sigma=diag( sigma.*sigma ) ;      
    [junk,priorpdf(1)]=mnormalpdf(param(prior(2:numn+1,1)),mu,sigma); 
end; 

% ======================================================
% Prior of an Inverse-Wishart or IG2 under independence
% =======================================================
numw=prior(1,2); 
if numw > 0; 
    extr=prior(2:numw+1,2);
    vv=alphap(extr);
    ss=betap(extr);
    sig2=param(extr).*param(extr); 
    ii=1;
    temp=zeros(numw,1); 
    for ii=1:numw; 
      [junk,temp(ii)]=invwishlub( sig2(ii), vv(ii) , ss(ii) ); 
    end 
    priorpdf(2)=sum(temp); 
end; 
% =============================================
% Prior of an Gamma density under independence
% =============================================
numg=prior(1,3);
if numg > 0;  
    extr=prior(2:numg+1,3);
    temp=feval(@gampdf,param(extr),alphap(extr),betap(extr));
    
    % AJ 3/05 
    if any( temp == 0 ) == 1 ; 
        priorpdf(3) = -inf ; 
        return 
    end 
    
    priorpdf(3)=sum(log(temp));     
end;
% =========================================
% Prior of Beta density under independence
% =========================================
numb=prior(1,4); 
% Sample Betas under independence 
if numb > 0;
    extr=prior(2:numb+1,4);
    temp=feval(@betapdf,param(extr),alphap(extr),betap(extr));
    
    % AJ 3/05 
    if any( temp == 0 ) == 1 ; 
        priorpdf(4) = -inf ; 
        return 
    end 
    
    priorpdf(4)=sum(log(temp));
end
% =========================================
% Prior of IG1 density under independence
% =========================================
numi=prior(1,5); 
if numi > 0;
    extr=prior(2:numi+1,5); 
    temp=zeros(numi,1); 
    for jj=1:numi; 
        [junk,temp(jj)]=feval(@pdf_igone,param(extr(jj) ) , alphap(extr(jj)), betap(extr(jj)) );
    end 
    priorpdf(5)=sum( temp ) ;
end 
% =====================
% Prior of a Uniform 
% ====================
numu=prior(1,6);
if numu > 0;
    extr = prior(2:numu+1,6);
    temp1= betap(extr) - alphap(extr);
    if any( temp1 < eps); error('Bounds for U wrongly defined');end; 
    tempd= 1./ temp1 ;
    priorpdf(6) =sum(log(tempd));
end
%% Prior for a Truncated B in the (a,b) interval 
if prior(1,7) > 0;
    if length(lowerb)~=length(param); 
        error('LOWERB and UPPERB seems to have been truncated before PRIORDENS')
    end 
    if nargin < 4; error('Need pass LOWERB and UPPERB for this density'); end 
    numt=prior(1,7); 
    % Row Indicators in the Full parameter vector 
    indt=prior(2:numt+1,7); 
    if any( param(indt) >  upperb(indt) )==1 || any( param(indt) <  lowerb(indt) )
        error('Parameter in Truncated BETA is outside bounds; check INDMAT or BOUNDS') 
    end 
    temp=( param(indt)-lowerb(indt) )./( upperb(indt)-lowerb(indt) ); 
    priorpdf(7)=sum( log( ( feval(@betapdf,temp,alphap(indt),betap(indt)))./(  upperb(indt)-lowerb(indt) ) ) );
    % Checked 1/13/09
end

% =====================
% Weibull Prior
% =====================
nume=prior(1,8);
if nume > 0;
    extr = prior(2:numu+1,8);
    temp=feval(@wblpdf,param(extr),alphap(extr),betap(extr));
    priorpdf(8)=sum( log(temp) );
end
end 

